<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>xeokit Example</title>
    <link href="../css/pageStyle.css" rel="stylesheet"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/js/all.min.js"></script>
</head>
<body>
<input type="checkbox" id="info-button"/>
<label for="info-button" class="info-button"><i class="far fa-3x fa-question-circle"></i></label>
<canvas id="myCanvas"></canvas>
<div class="slideout-sidebar">
    <h1>Picking Objects</h1>
    <h2>Click objects to pick them</h2>
    <h3>Components Used</h3>
    <ul>
        <li>
            <a href="../../docs/class/src/viewer/Viewer.js~Viewer.html"
               target="_other">Viewer</a>
        </li>
        <li>
            <a href="../../docs/class/src/plugins/XKTLoaderPlugin/XKTLoaderPlugin.js~XKTLoaderPlugin.html"
               target="_other">XKTLoaderPlugin</a>
        </li>
    </ul>
</div>
</body>
<script type="module">

    import {Viewer, XKTLoaderPlugin} from "../../dist/xeokit-sdk.min.es.js";

    const viewer = new Viewer({
        canvasId: "myCanvas",
        dtxEnabled: true // Enable data texture model representation
    });

    viewer.camera.eye = [-3.93, 2.85, 27.01];
    viewer.camera.look = [4.40, 3.72, 8.89];
    viewer.camera.up = [-0.01, 0.99, 0.039];

    const xktLoader = new XKTLoaderPlugin(viewer);

    const sceneModel = xktLoader.load({
        id: "myModel",
        src: "../../assets/models/xkt/v10/glTF-Embedded/Duplex_A_20110505.glTFEmbedded.xkt",
        edges: true
    });

    //------------------------------------------------------------------------------------------------------------------
    // Mouse hover to snap to vertices
    //------------------------------------------------------------------------------------------------------------------

    viewer.scene.highlightMaterial.fill = false;
    viewer.scene.highlightMaterial.edgeAlpha = 0.5;

    const markerDiv = document.createElement('div');
    const canvas = viewer.scene.canvas.canvas;
    canvas.parentNode.insertBefore(markerDiv, canvas);

    markerDiv.style.background = "black";
    markerDiv.style.border = "2px solid blue";
    markerDiv.style.borderRadius = "20px";
    markerDiv.style.width = "10px";
    markerDiv.style.height = "10px";
    markerDiv.style.margin = "-200px -200px";
    markerDiv.style.zIndex = "100";
    markerDiv.style.position = "absolute";
    markerDiv.style.pointerEvents = "none";

    // Mouse input

    var lastEntity = null;

    function updateCursorPosition(canvasPos) {
        const pickResult = viewer.scene.pick({
            canvasPos,
            snapRadius: 30,
            snapToEdge: false, // Default is true
            snapToVertex: true // Default is true
        });
        if (pickResult && pickResult.snappedCanvasPos) {
            markerDiv.style.marginLeft = `${pickResult.snappedCanvasPos[0] - 10}px`;
            markerDiv.style.marginTop = `${pickResult.snappedCanvasPos[1] - 10}px`;
            markerDiv.style.background = "greenyellow";
            markerDiv.style.border = "3px solid green";

            if (!lastEntity || (pickResult.entity && pickResult.entity.id !== lastEntity.id)) {
                if (lastEntity) {
                    lastEntity.highlighted = false;
                }
                lastEntity = pickResult.entity;
                if (pickResult.entity) {
                    pickResult.entity.highlighted = true;
                }
            }
        } else {
            markerDiv.style.marginLeft = `${canvasPos[0] - 10}px`;
            markerDiv.style.marginTop = `${canvasPos[1] - 10}px`;
            markerDiv.style.background = "white";
            markerDiv.style.border = "1px solid black";

            if (lastEntity) {
                lastEntity.highlighted = false;
                lastEntity = null;
            }
        }
    }

    function mouseUpdateCursorPosition(event) {
        event.preventDefault();
        updateCursorPosition([event.clientX, event.clientY]);
    }

    function touchUpdateCursorPosition(event) {
        event.preventDefault();
        const touch = event.touches[0];
        updateCursorPosition([touch.clientX, touch.clientY]);
    }

    document.addEventListener("mousemove", mouseUpdateCursorPosition);
    document.addEventListener("touchstart", touchUpdateCursorPosition);
    document.addEventListener("touchmove", touchUpdateCursorPosition);

</script>
</html>